//
//  ChangePhoneViewController.m
//  阿帕
//
//  Created 阿帕 on 2020/2/5.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "ChangePhoneViewController.h"

@interface ChangePhoneViewController ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UITextField *oldTelTextField;
@property (weak, nonatomic) IBOutlet UITextField *xinTelTextField;
@property (weak, nonatomic) IBOutlet UITextField *confTelTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;


@end

@implementation ChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"修改手机号";
    
    self.view.backgroundColor = BackgroundColor;
    
    _xinTelTextField.textFieldType = CJTextFieldStringTypeNumber;
    _xinTelTextField.maxLength = 11;
    
    _confTelTextField.textFieldType = CJTextFieldStringTypeNumber;
    _confTelTextField.maxLength = 11;
    
//    _passwordTextField.textFieldType = CJTextFieldStringTypePassword;
    _passwordTextField.maxLength = 18;
    
    _oldTelTextField.text = _oldPhone;
    
    cut(_confirmBtn, BTN_BORDER_RADIUS);
    [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_confirmBtn setBackgroundColor:Theme_ColorValue];
    
    for (int i=0; i<4; i++) {
        UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, 45*i, ScreenW-30, 1)];
        hLine.backgroundColor = BackgroundColor;
        [_bgView addSubview:hLine];
    }
}
- (IBAction)confirmBtnClick:(id)sender {
    WEAKSELF
   
    if (_oldTelTextField.text.length != 11 || _xinTelTextField.text.length != 11 || _confTelTextField.text.length != 11) {
        [XSInfoView showInfo:@"手机号必须是11位" onView:self.view];
        return;
    }
    if ([DCSpeedy isBlankString:_passwordTextField.text] == YES) {
        [XSInfoView showInfo:@"请输入密码" onView:self.view];
        return;
    }
    if (![_xinTelTextField.text isEqual:_confTelTextField.text]) {
        [XSInfoView showInfo:@"两次新手机号不一致" onView:self.view];
        return;
    }
    if (_passwordTextField.text.length > 20) {
        [XSInfoView showInfo:@"密码最长为20位" onView:self.view];
        return;
    }
    
    NSDictionary *parmas = @{
        @"oldPhone": _oldTelTextField.text,
        @"newPhone": _xinTelTextField.text,
//        @"pwd": _passwordTextField.text,
        @"pwd": [DCSpeedy base64:_passwordTextField.text],
        @"branchCode": [UserDefaultUtil valueForKey:BranchCode],
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    
    [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], PARTY_UPDATEPHONE) withParameter:parmas withVC:self netName:@"修改手机号" success:^(id response, NSString *msg) {
        
        [XSInfoView showInfo:msg onView:self.view];
        
        [KNotificationCenter postNotificationName:LOGIN_SUCCESS object:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        });
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
